import { apiUr } from './api';
import { defineStore } from 'pinia';
import { PropType } from 'vue';
import { wxlogin } from './util';
import { number } from 'echarts';
const token = uni.getStorageSync('token') || "";


export const useStoreUser = defineStore('useUser', {
	state: () => {
		return {
			uploadUrl: uni.hostUrl + '/public/uploadImg',
			userinfo: {
				id: "",
				"auth": {
					"auth_type": 1,
					"auth_text": "",
					"auth_state": false,
					"auth_state_text": ""
				},
				"company_name": "",
				"realname": "",
				"mobile": ""
			} as MyApp.userInfo,
			token: token,
		}
	},
	getters: {
		getToken() {
			this.token = uni.getStorageSync('token') || ""
			return this.token;
		}
	},
	actions: {
		async login() {
			var token = this.token;
			if (!token) {
				token = await wxlogin();
			}
			this.token = token;
			uni.$tm.u.setCookie('token', token)
		},
		async getUserInfo(): Promise<MyApp.userInfo | undefined> {
			// if (!this.token) return undefined;
			let t = this;
			uni.showLoading({ title: '...' })
			const userinfo = await apiUr.getUserInfo().finally(() => {
				uni.hideLoading()
			})
			if (!userinfo) return undefined;
			this.userinfo = userinfo.data;
			return this.userinfo;
		},
		async loginOut() {
			// await apiUr.loginOut()
			// uni.removeStorageSync("token");
			// this.token = null;
			// this.userinfo = undefined;
			// uni.reLaunch({
			// 	url: "/pages/user/login"
			// })

		},

	},
});